
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class Login extends Form implements CommandListener, Runnable {

    private TextField txtUsername;
    private TextField txtPassword;
    private ChoiceGroup choiceGroup;
    private Command cmdLogin;
    private Command cmdExit;
    private Command cmdRegister;
    private UpAnh upAnh;

    public Login(UpAnh upAnh) {
        super("Login");
        this.upAnh = upAnh;
        txtUsername = new TextField("Tai khoan", "", 30, TextField.ANY);
        txtPassword = new TextField("Mat khau", "", 30, TextField.PASSWORD);
        choiceGroup = new ChoiceGroup("", Choice.MULTIPLE);
        try {
            RecordStore recordStore = RecordStore.openRecordStore("LuuTaiKhoan", false);
            txtUsername.setString(new String(recordStore.getRecord(1)));
            txtPassword.setString(new String(recordStore.getRecord(2)));
            recordStore.closeRecordStore();
        } catch (Exception e) {
        }
        choiceGroup.append("Luu tai khoan", null);
        this.append(txtUsername);
        this.append(txtPassword);
        this.append(choiceGroup);
        cmdLogin = new Command("Dang nhap", Command.OK, 2);
        cmdExit = new Command("Thoat", Command.EXIT, 2);
        cmdRegister = new Command("Dang ky", Command.SCREEN, 2);
        this.addCommand(cmdLogin);
        this.addCommand(cmdExit);
        this.addCommand(cmdRegister);
        this.setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdLogin) {
            if (!Check()) {
                upAnh.client.Login(txtUsername.getString(), txtPassword.getString());
                new Thread(this).start();
            }
            if (choiceGroup.isSelected(0)) {
                SaveTaikhoan();
            }
        } else if (c == cmdExit) {
            upAnh.Exit();
        } else if (c == cmdRegister) {
            upAnh.DisplayRegister();
        }
    }

    private void SaveTaikhoan() {
        try {
            RecordStore recordStore = RecordStore.openRecordStore("LuuTaiKhoan", true);
            recordStore.addRecord(txtUsername.getString().getBytes(), 0, txtUsername.getString().length());
            recordStore.addRecord(txtPassword.getString().getBytes(), 0, txtPassword.getString().length());
            recordStore.closeRecordStore();
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    private boolean Check() {
        boolean isError = false;
        if (txtUsername.getString().equals("")) {
            this.append("Ban chua nhap tai khaon");
            isError = true;
        }
        if (txtPassword.getString().equals("")) {
            this.append("Ban chua nhap mat khau");
            isError = true;
        }
        return isError;
    }

    public void run() {
        while (true) {
            if (upAnh.UserId > 0) {
                upAnh.DisplayHomeMain();
                break;
            } else if (upAnh.UserId == -1) {
                upAnh.DisplayError(ErrorType.LoginnFails);
                break;
            }
            System.gc();
        }
    }
}
